# @file    robot_startup.launch.py
# @brief   launch file for startup each module of robot
# @author  Mr.Wu
# @date    2025-06-06

# Copyright 2025 Mr.Wu

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.conditions import IfCondition
import os

def generate_launch_description():
    # robot_base parameters
    port_name_arg=DeclareLaunchArgument("port_name", default_value="ttyS1", description="Serial port name")
    baudrate_arg=DeclareLaunchArgument("baudrate", default_value="115200", description="Serial port baudrate")
    correct_factor_vx_arg=DeclareLaunchArgument("correct_factor_vx", default_value="1.0", description="Velocity correction factor")
    correct_factor_wz_arg=DeclareLaunchArgument("correct_factor_wz", default_value="1.0", description="Angular velocity correction factor")
    pub_odom_tf_arg=DeclareLaunchArgument("pub_odom_tf", default_value="true", description="Publish odometry transform")
    use_imu_arg=DeclareLaunchArgument("use_imu",default_value="false",description="Use IMU data")
    auto_stop_on_arg=DeclareLaunchArgument("auto_stop_on",default_value="true",description="Auto stop on signal")
    # arguments to enable/disable modules
    use_lidar_arg=DeclareLaunchArgument("use_lidar", default_value="false", description="Whether to use lidar or not")
    use_camera_arg=DeclareLaunchArgument("use_camera", default_value="false", description="Whether to use camera or not")

    # robot_base node
    robot_base_pkg=get_package_share_directory('robot_base')
    robot_base_launch=os.path.join(robot_base_pkg,"launch","robot.launch.py")
    robot_base_node=IncludeLaunchDescription(
        PythonLaunchDescriptionSource(robot_base_launch),
        launch_arguments={
            "port_name":LaunchConfiguration("port_name"),
            "baudrate":LaunchConfiguration("baudrate"),
            "correct_factor_vx":LaunchConfiguration("correct_factor_vx"),
            "correct_factor_wz":LaunchConfiguration("correct_factor_wz"),
            "pub_odom_tf":LaunchConfiguration("pub_odom_tf"),
            "use_imu":LaunchConfiguration("use_imu"),
            "auto_stop_on":LaunchConfiguration("auto_stop_on")
        }.items())

    # lodar node
    lidar_pkg=get_package_share_directory('lidar_driver')
    lidar_launch=os.path.join(lidar_pkg,"launch","vp100_launch.py")
    lidar_node=IncludeLaunchDescription(
        PythonLaunchDescriptionSource(lidar_launch),
        condition=IfCondition(LaunchConfiguration("use_lidar"))
    )

    # camera node
    camera_pkg=get_package_share_directory('camera_driver')
    camera_launch=os.path.join(camera_pkg,"launch","camera.launch.py")
    camera_node=IncludeLaunchDescription(
        PythonLaunchDescriptionSource(camera_launch),
        condition=IfCondition(LaunchConfiguration("use_camera"))
    )

    ld=LaunchDescription()
    ld.add_action(port_name_arg)
    ld.add_action(baudrate_arg)
    ld.add_action(correct_factor_vx_arg)
    ld.add_action(correct_factor_wz_arg)
    ld.add_action(pub_odom_tf_arg)
    ld.add_action(use_imu_arg)
    ld.add_action(auto_stop_on_arg)
    ld.add_action(use_lidar_arg)
    ld.add_action(use_camera_arg)
    ld.add_action(robot_base_node)
    ld.add_action(lidar_node)
    ld.add_action(camera_node)

    return ld